package cn.jsj.machine.exceptionController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@ResponseBody
@ControllerAdvice
public class ExceptionController {

    @ExceptionHandler(value = Exception.class)
    public Map<String,Object> illegalArgumentException(HttpServletRequest request, HttpServletResponse response, Exception e ){
        Logger logger = LoggerFactory.getLogger(this.getClass());
        String url = request.getRequestURL().toString();
        Map<String, String[]> storage = request.getParameterMap();
        StringBuffer stringBuffer = new StringBuffer("");
        stringBuffer.append("请求路径："+url);
        for(String i: storage.keySet()){
            stringBuffer.append("参数"+i+":");
            String[] para = storage.get(i);
            if(para == null){
                continue;
            }
            for(String j:para){
                stringBuffer.append(j+" ");
            }
        }
        logger.error(stringBuffer.toString()+e.toString());
        Map<String,Object> result = new HashMap<>();
        result.put("state",-1);
        result.put("message","请联系程序开发人员");
        result.put("data",null);
        result.put("rows",null);
        result.put("total",0);
        return result;
    }
}
